package com.yc.psychologicalconsultant.controller;

import com.yc.psychologicalconsultant.vo.*;
import com.yc.psychologicalconsultant.pojo.Result;
import com.yc.psychologicalconsultant.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation("通过用户名登录")
    @PostMapping("/login/username")
    public Result loginUserName(@RequestBody LoginUserNameVO loginUserNameVO) {
        log.info("登录  账号{}  密码{}", loginUserNameVO.getUsername(), loginUserNameVO.getPassword());
        return userService.loginByUsername(loginUserNameVO);
    }

    @ApiOperation("通过手机号登录")
    @PostMapping("/login/phone")
    public Result registerByPhone(@Validated @RequestBody LoginPhoneVO loginPhoneVO) {
        log.info("登录操作 phone={} , code={}", loginPhoneVO.getPhone(), loginPhoneVO.getCode());
        return userService.loginOrRegisterByPhone(loginPhoneVO);
    }

    @ApiOperation("通过邮箱登录")
    @PostMapping("/login/email")
    public Result loginEmail(@RequestBody LoginEmailVO loginEmailVO) {
        log.info("登录 邮箱{}, 验证码{}", loginEmailVO.getEmail(), loginEmailVO.getCode());
        return userService.loginByEmail(loginEmailVO);
    }

    @ApiOperation("邮箱注册")
    @PostMapping("/register/email")
    public Result registerByEmail(@Validated @RequestBody UserRegisterByEmailVO user) {
        log.info("注册操作username={} ,password={} ,email={} , code={}", user.getUsername(),
                user.getPassword(), user.getEmail(), user.getCode());
        return userService.registerByEmail(user);
    }

    @ApiOperation("获取邮箱验证码")
    @PostMapping("/verification/email")
    public Result verification(@Validated @RequestBody VerificationEmailVO verificationEmailVO) throws Exception {
        return userService.getEmailCode(verificationEmailVO);
    }

    @ApiOperation("获取手机验证码")
    @PostMapping("/verification/phone/{phone}")
    public Result verificationPhone(@ApiParam(value = "手机号码", required = true)
                                    @Validated @PathVariable String phone) {
        return userService.getPhoneCode(phone);
    }

    @ApiOperation("查找某用户")
    @GetMapping("/search/user")
    public Result searchUser(@RequestParam String username) {
        return userService.searchUser(username);
    }
}
